package procsim;

import java.util.*;

public class Tick {
    private static ArrayList<GSignal> signals;
    public static void tick(Program parent, boolean nf){
        if(!nf) populateActiveSignalsList();
        // CU OP
        // pomocni
        Design.OPORrun.tick();
        Design.OPAND2.tick();
        Design.OPAND12.tick();
        // glavni
        Design.OPOR1.tick();
        Design.OPOR2.tick();
        Design.OPOR3.tick();
        Design.OPOR4.tick();
        Design.OPOR5.tick();
        Design.OPOR6.tick();
        Design.OPOR7.tick();
        Design.OPOR8.tick();
        Design.OPOR9.tick();
        Design.OPOR10.tick();
        Design.OPOR11.tick();
        Design.OPOR12.tick();
        Design.OPOR13.tick();
        Design.OPOR14.tick();
        Design.OPOR15.tick();
        Design.OPOR16.tick();
        Design.OPOR17.tick();
        Design.OPOR18.tick();
        Design.OPOR19.tick();
        Design.OPOR20.tick();
        Design.OPOR21.tick();
        Design.OPOR22.tick();
        Design.OPOR23.tick();
        Design.OPOR24.tick();
        Design.OPOR25.tick();
        Design.OPOR26.tick();
        Design.OPOR27.tick();
        Design.OPOR28.tick();
        Design.OPOR29.tick();
        Design.OPOR30.tick();
        Design.OPOR31.tick();
        Design.OPOR32.tick();
        Design.OPOR33.tick();
        Design.OPOR34.tick();
        Design.OPOR35.tick();
        Design.OPOR36.tick();
        // CU CU
        Design.SCInv1.tick();
        Design.SCInv2.tick();
        Design.SCInv3.tick();
        Design.SCInv4.tick();
        Design.SCInv5.tick();
        Design.SCInv6.tick();
        Design.SCUAND1.tick();
        Design.SCUAND2.tick();
        Design.SCUAND3.tick();
        Design.SCUAND4.tick();
        Design.SCUAND5.tick();
        Design.SCUAND6.tick();
        Design.SCUAND7.tick();
        Design.SCUAND8.tick();
        Design.SCUAND9.tick();
        Design.SCUAND10.tick();
        Design.SCUAND11.tick();
        Design.SCUAND12.tick();
        Design.SCUAND13.tick();
        Design.SCUAND14.tick();
        Design.SCUAND15.tick();
        Design.SCUAND16.tick();
        Design.SCUAND17.tick();
        Design.SCOR1.tick();
        Design.SCOR2.tick();
        Design.SCOR3.tick();
        Design.SCOR4.tick();
        Design.SCOR5.tick();
        Design.SCOR6.tick();
        Design.SCOR7.tick();
        Design.SCOR8.tick();
        Design.SCOR9.tick();
        Design.SCOR10.tick();
        Design.SCOR11.tick();
        Design.SCOR12.tick();
        Design.SCOR13.tick();
        Design.SCOR14.tick();
        Design.SCOR15.tick();

        // CUOPR
        Design.CUOPRAND1.tick();
        Design.CUOPRAND2.tick();
        Design.CUOPRAND3.tick();
        Design.CUOPRAND4.tick();
        Design.CUOPRAND5.tick();
        Design.CUOPRCoder.tick();
        Design.CUOPRMP.tick();

        // CUBRANCH
        Design.CUBRANCHCoder.tick();
        Design.CUBRANCHMP.tick();

        // Combinational

        // MARMDR
        Design.mxMAR.tick();
        Design.mxMDR.tick();
        Design.MDROR.tick();
        Design.MDRbusAND.tick();
        Design.TSRDBUS.tick();
        Design.TSWRBUS.tick();
        Design.rdbusAND.tick();
        Design.wrbusAND.tick();
        Design.TSMAR.tick();
        Design.TSMDR.tick();
        // IF
        Design.IFDC1.tick();
        Design.IFDC2.tick();
        Design.IFInv1.tick();
        Design.IFInv2.tick();
        Design.IFInv3.tick();
        Design.IFAND1.tick();
        Design.IFAND2.tick();
        Design.IFOR1.tick();
        Design.IFOR2.tick();
        Design.IFOR3.tick();
        Design.IFOR4.tick();
        Design.IFOR5.tick();
        Design.IFOR6.tick();
        // ID
        Design.IDOR1.tick();
        Design.IDDC1.tick();
        Design.IDInv1.tick();
        Design.IDAND1.tick();
        Design.IDDC2.tick();
        Design.IDOR3.tick();
        Design.IDOR4.tick();
        Design.IDOR5.tick();
        Design.IDInv3.tick();
        Design.IDInv4.tick();
        Design.IDAND9.tick();
        Design.IDAND6.tick();
        Design.IDAND7.tick();
        Design.IDDC3.tick();
        Design.IDInv2.tick();
        Design.IDAND8.tick();
        Design.IDDC4.tick();
        Design.IDInv5.tick();
        Design.IDAND2.tick();
        Design.IDAND3.tick();
        Design.IDInv6.tick();
        Design.IDAND4.tick();
        Design.IDAND5.tick();
        Design.IDOR2.tick();
        Design.IDOR6.tick();
        Design.IDOR7.tick();
        Design.IDOR8.tick();
        Design.IDInv7.tick();
        Design.IDAND10.tick();
        Design.IDOR9.tick();
        Design.IDInv9.tick();
        Design.IDInv8.tick();
        Design.IDAND11.tick();
        Design.IDOR10.tick();
        Design.IDAND13.tick();
        Design.IDOR11.tick();
        Design.IDOR12.tick();
        Design.IDAND14.tick();
        Design.IDAND15.tick();
        Design.IDOR13.tick();
        // Aux
        Design.AuxOR1.tick();
        Design.AuxOR2.tick();
        Design.AuxOR3.tick();
        Design.AuxOR4.tick();
        Design.AuxOR5.tick();
        Design.AuxOR6.tick();
        Design.AuxAND1.tick();
        Design.AuxAND2.tick();
        Design.AuxAND3.tick();
        Design.AuxAND4.tick();
        Design.AuxAND5.tick();
        // Arbitrator
        Design.ATriStateLeft2.tick();
        Design.ATriStateLeft1.tick();
        Design.AAndLeft.tick();
        Design.ATriStateRight.tick();
        Design.AOr.tick();
        Design.AInverter.tick();
        Design.AAndRight.tick();
        Design.ACoder.tick();
        Design.ADecoder.tick();
        // DISP
        Design.DISPAND1.tick();
        Design.DISPAND2.tick();
        Design.DISPOR1.tick();
        Design.DISPOR2.tick();
        Design.DISPOR3.tick();
        Design.DISPMP1.tick();
        Design.DISPMP2.tick();
        Design.DISPMP3.tick();
        Design.DISPMP4.tick();
        Design.DISPADD.tick();
        // IMR
        Design.IMROR.tick();
        Design.IMRInv1.tick();
        Design.IMRInv2.tick();
        Design.IMRInv3.tick();
        Design.IMRAND1.tick();
        Design.IMRAND2.tick();
        Design.IMRAND3.tick();
        Design.IMRAND4.tick();
        Design.IMRAND5.tick();
        Design.IMRAND6.tick();
        Design.IMRAND7.tick();
        Design.IMRAND8.tick();
        // Interrupts
        Design.ICoder.tick();
        Design.IMMP1.tick();
        Design.IMMP2.tick();
        Design.IOrUpper.tick();
        Design.IOrLower.tick();
        Design.IAnd1.tick();
        Design.IAnd2.tick();
        Design.IAnd3.tick();
        Design.IAdd.tick();
        Design.IDecoder.tick();
        // MEM1
        Design.MTriState1.tick();
        Design.MTriState2.tick();
        Design.MOr1.tick();
        Design.MOr2.tick();
        Design.MOr3.tick();
        Design.MTriState3.tick();
        // MEM2
        Design.M2Or1.tick();
        Design.M2Or2.tick();
        Design.M2And.tick();
        // Masked IRQ
        Design.MILeft.tick();
        Design.MIMiddle.tick();
        Design.MIRight.tick();
        Design.MIOr.tick();
        Design.MICoder.tick();
        Design.MIComparator.tick();
        Design.MIFarRight.tick();
        // Masked IRQ ACK
        Design.MIAAnd1.tick();
        Design.MIAAnd2.tick();
        Design.MIAAnd3.tick();
        Design.MIAAnd4.tick();
        // PC
        Design.PCHMP.tick();
        Design.PCLMP.tick();
        // PSW1
        Design.PSW1Inv1.tick();
        Design.PSW1Inv2.tick();
        Design.PSW1Inv3.tick();
        Design.PSW1Inv4.tick();
        Design.PSW1Inv5.tick();
        Design.PSW1Inv6.tick();
        Design.PSW1And1.tick();
        Design.PSW1And2.tick();
        Design.PSW1And3.tick();
        Design.PSW1And4.tick();
        Design.PSW1And5.tick();
        Design.PSW1And6.tick();
        Design.PSW1And7.tick();
        Design.PSW1And8.tick();
        Design.PSW1And9.tick();
        Design.PSW1And10.tick();
        Design.PSW1And11.tick();
        Design.PSW1And12.tick();
        Design.clPSWTAnd.tick();
        Design.stPSWTAnd.tick();
        Design.clPSWIAnd.tick();
        Design.stPSWIAnd.tick();
        Design.PSW1Or1.tick();
        Design.PSW1Or2.tick();
        Design.PSW1Or3.tick();
        Design.PSW1Or4.tick();
        Design.PSW1Or5.tick();
        Design.PSW1Or6.tick();
        Design.PSW1Or7.tick();
        Design.PSW1Or8.tick();
        Design.clPSWTOr.tick();
        Design.clPSWIOr.tick();
        // PSW2
        Design.PSW2Inv1.tick();
        Design.PSW2Inv2.tick();
        Design.PSW2Inv3.tick();
        Design.PSW2Inv4.tick();
        Design.PSW2Inv5.tick();
        Design.PSW2Inv6.tick();
        Design.PSW2Inv7.tick();
        Design.PSW2Inv8.tick();
        Design.PSW2CondInv.tick();
        Design.PSW2And1.tick();
        Design.PSW2And2.tick();
        Design.PSW2And3.tick();
        Design.PSW2And4.tick();
        Design.PSW2And5.tick();
        Design.PSW2And6.tick();
        Design.PSW2And7.tick();
        Design.PSW2And8.tick();
        Design.PSW2And9.tick();
        Design.PSW2And10.tick();
        Design.PSW2And11.tick();
        Design.PSW2And12.tick();
        Design.PSW2And13.tick();
        Design.PSW2And14.tick();
        Design.PSW2And15.tick();
        Design.PSW2And16.tick();
        Design.PSW2CondAnd.tick();
        Design.PSW2Or1.tick();
        Design.PSW2Or2.tick();
        Design.PSW2Or3.tick();
        Design.PSW2Or4.tick();
        Design.PSW2Or5.tick();
        Design.PSW2Or6.tick();
        Design.PSW2Or7.tick();
        Design.PSW2Or8.tick();
        // Registers 1
        Design.Reg1MP1.tick();
        Design.Reg1DC.tick();
        Design.Reg1CD.tick();
        Design.Reg1MP3.tick();
        Design.Reg1MP2.tick();
        Design.Reg1MP4.tick();
        Design.Reg1MP5.tick();
        // Registers 2
        Design.Reg2DC.tick();
        Design.RegOR.tick();
        Design.Reg2MP1.tick();
        Design.Reg2ADD.tick();
        Design.Reg2MP2.tick();
        // Sync
        Design.SyncAND1.tick();
        Design.SyncAND2.tick();
        Design.SyncAND3.tick();
        Design.SyncAND4.tick();
        Design.SyncOR1.tick();
        Design.SyncAND5.tick();
        Design.SyncOR2.tick();
        Design.SyncInv1.tick();
        Design.SyncInv2.tick();
        Design.SyncAND6.tick();
        Design.SyncAND7.tick();
        Design.SyncAND8.tick();
        Design.SyncOR3.tick();
        Design.SyncOR4.tick();
        Design.SyncOR5.tick();
        // X & Y
        Design.XYInv.tick();
        Design.XYAND.tick();
        Design.XYOR1.tick();
        Design.XYOR2.tick();
        Design.XYMP1.tick();
        Design.XYMP2.tick();
        Design.XYMP3.tick();
        // ALU
        Design.ALUAND1.tick();
        Design.ALUAND2.tick();
        Design.ALUAND3.tick();
        Design.ALUAND4.tick();
        Design.ALUAND5.tick();
        Design.ALUAND6.tick();
        Design.ALU.tick();
        // Indicators
        Design.AIOR1.tick();
        Design.AIInv1.tick();
        Design.AIAND1.tick();
        Design.AIAND2.tick();
        Design.AIAND3.tick();
        Design.AIOR2.tick();
        Design.AIInv2.tick();
        Design.AIAND4.tick();
        Design.AIInv3.tick();
        Design.AIInv4.tick();
        Design.AIAND5.tick();
        Design.AIOR3.tick();
        Design.AIAND6.tick();
        Design.AIInv5.tick();
        Design.AIAND7.tick();
        Design.AIInv6.tick();
        Design.AIAND8.tick();
        Design.AIOR4.tick();
        Design.AIOR5.tick();
        // CU
        Design.CUOr.tick();
        Design.CUInv.tick();
        Design.CUAnd1.tick();
        Design.CUAnd2.tick();
        Design.CUMP.tick();
        Design.CUDC.tick();

        // buses
        for(int i=0; i<Design.buses.length; i++)
            Design.buses[i].tick();

        // Sequential
        
        // CU
        Design.CUCNT.tick();
        
        Register.beginTicking();
        FlipFlop.beginTicking();

        // Arbitrator
        Design.ARSLeft.ammendR(Design.brqSTOP);
        Design.ARSRight.ammendR(Design.brqSTOP);
        Design.ARSLeft.tick();
        Design.ARSRight.tick();
        // Sync
        Design.SyncD1.tick();
        Design.SyncD2.tick();
        Design.SyncD3.tick();
        Design.SyncCNT.tick();
        
        // Interrupts
        Design.IVTP.tick();
        // DISP
        Design.TEMPDISP.tick();
        // MARMDR
        Design.MAR.ammendInc(Design.incMAR);
        Design.MAR.tick();
        // IF
        Design.IR_1.tick();
        Design.IR_2.tick();
        Design.IR_3.tick();
        Design.IR_4.tick();
        // PC
        Design.PCH.tick();
        Design.PCL.tick();
        Design.PC.tick();
        Design.ADRTMP.tick();
        // X & Y
        Design.XH.tick();
        Design.XL.tick();
        Design.YH.tick();
        Design.YL.tick();
        // ALU
        Design.TEMP.tick();
        Design.ALURS.tick();
        // Masked IRQ ACK
        Design.MIARSLeft1.tick();
        Design.MIARSLeft2.tick();
        Design.MIARSLeft3.tick();
        Design.MIARSLeft4.tick();
        Design.MIARSRight1.tick();
        Design.MIARSRight2.tick();
        Design.MIARSRight3.tick();
        Design.MIARSRight4.tick();
        // PSW1
        Design.PSW1RS1.tick();
        Design.PSW1RS2.tick();
        Design.PSW1RS3.tick();
        Design.PSW1RS4.tick();
        // PSW2
        Design.PSW2RS1.tick();
        Design.PSW2RS2.tick();
        Design.PSW2RS3.tick();
        Design.PSW2RS4.tick();
        // Registers 2
        Design.AX.tick();
        Design.BX.tick();
        Design.CX.tick();
        Design.DX.tick();
        Design.SP.ammendInc(Design.incSP);
        Design.SP.tick();
        Design.BP.tick();
        Design.SI.tick();
        Design.DI.tick();
        Design.RegTMP.tick();
        // IMR
        Design.IMRRS1.tick();
        Design.IMRRS2.tick();
        Design.IMRRS3.tick();
        Design.PRINTRS.tick();
        Design.PRADRRS.tick();
        Design.PROPRRS.tick();

        Register.endTicking();
        FlipFlop.endTicking();
        
        // buses
        for(int i=0; i<Design.buses.length; i++)
            Design.buses[i].tick();

        Register.beginTicking();
        FlipFlop.beginTicking();

        // MEM1
        Design.MRS1.ammendR(Design.FC);
        Design.MRS2.ammendR(Design.FC);
        Design.MRS1.tick();
        Design.MRS2.tick();
        Design.memory.tick();
        // MEM2
        Design.CNT.tick();
        Design.M2DFF.tick();
        // MEM1 back
        Design.DR.tick();

        Register.endTicking();
        FlipFlop.endTicking();

        // buses
        for(int i=0; i<Design.buses.length; i++)
            Design.buses[i].tick();

        // MARMDR back
        Design.MDR.tick();
        
        setActiveSignals(parent);
    }
    private static void populateActiveSignalsList(){
        signals = new ArrayList<GSignal>();
        signals.add(Design.ldMAR);
        signals.add(Design.incMAR);
        signals.add(Design.mxMAR0);
        signals.add(Design.mxMAR1);
        signals.add(Design.mxMAR2);
        signals.add(Design.ldMDR);
        signals.add(Design.mxMDR0);
        signals.add(Design.mxMDR1);
        signals.add(Design.mxMDR2);
        signals.add(Design.read);
        signals.add(Design.write);
        signals.add(Design.incSP);
        signals.add(Design.decSP);
        signals.add(Design.ldPCL);
        signals.add(Design.ldPCH);
        signals.add(Design.ldPC);
        signals.add(Design.mxPC0);
        signals.add(Design.mxPC1);
        signals.add(Design.mxPC2);
        signals.add(Design.incPC);
        signals.add(Design.R0);
        signals.add(Design.R1);
        signals.add(Design.R2);
        signals.add(Design.R3);
        signals.add(Design.R4);
        signals.add(Design.R5);
        signals.add(Design.R6);
        signals.add(Design.R7);
        signals.add(Design.ldXL);
        signals.add(Design.ldXH);
        signals.add(Design.ldYL);
        signals.add(Design.ldYH);
        signals.add(Design.transX);
        signals.add(Design.ldTEMP);
        signals.add(Design.aluOJO);
        signals.add(Design.aluODO);
        signals.add(Design.ldFLAGS);
        signals.add(Design.ldREG);
        signals.add(Design.ldINTEXT);
        signals.add(Design.ldADRTMP);
        signals.add(Design.ldIR1);
        signals.add(Design.ldIR2);
        signals.add(Design.ldIR3);
        signals.add(Design.ldIR4);
        signals.add(Design.stPRINT);
        signals.add(Design.ldPRADROPR);
        signals.add(Design.ldCONTROL);
        signals.add(Design.PSWin);
        signals.add(Design.ack);
        signals.add(Design.ldTD);
        signals.add(Design.ldTM);
        signals.add(Design.branch);
        signals.add(Design.bruncnd);
        signals.add(Design.brgrADROPR);
        signals.add(Design.brnotPREKID);
        signals.add(Design.brnotmemind);
        signals.add(Design.brmemind);
        signals.add(Design.brnotregip);
        signals.add(Design.brnotreg);
        signals.add(Design.brnotimmreg);
        signals.add(Design.brNAD2);
        signals.add(Design.brOPR1);
        signals.add(Design.brOPR2);
        signals.add(Design.brOPR);
        signals.add(Design.brL1);
        signals.add(Design.brL2);
        signals.add(Design.brL3);
        signals.add(Design.brbxsi);
        signals.add(Design.brbxdi);
        signals.add(Design.brbpsi);
        signals.add(Design.brbpdi);
        signals.add(Design.brnotCOND);
        signals.add(Design.brnotNAD);
    }
    private static void setActiveSignals(Program parent){
        ArrayList<String> activeSignals = new ArrayList<String>();
        String active="";
        Iterator iter = signals.iterator();
        while(iter.hasNext()){
            GSignal tmp = (GSignal)iter.next();
            if(!tmp.isNull())
                activeSignals.add(tmp.getName());
        }
        Collections.sort(activeSignals);
        iter = activeSignals.iterator();
        while(iter.hasNext())
            active += (String)iter.next() + (iter.hasNext()? ", " : "");
        parent.setActiveSignals(active);
    }
}
